//
//  SingleHRVDetailController.swift
//  DaRings
//
//  Created by 易先生 on 2023/2/18.
//

import UIKit
import MYModule

class SingleHRVDetailController: ViewController {

    public var provider: SingleHRVProvider?
    
    var showRightItem: Bool = true

    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigation.item.title = "hrv_title".language
        self.navigation.bar.backgroundColor = UIColor.background
    }
    
    override func makeUI() {
        super.makeUI()
        view.backgroundColor =  UIColor.darkBackground
        
        let bgView = View()
        bgView.backgroundColor = UIColor.background

        view.addSubview(scrollView)
        scrollView.addSubview(contentView)
        contentView.addSubview(bgView)
        contentView.addSubview(resultView)
        contentView.addSubview(explainView)
        contentView.addSubview(sevenTimeChartView)
        
        scrollView.snp.makeConstraints { make in
            make.top.equalTo(topLayoutGuide.snp.bottom)
            make.leading.trailing.bottom.equalToSuperview()
        }

        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        
        bgView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(explainView.snp.top)
            make.height.equalTo(10)
        }
        
        resultView.snp.makeConstraints { make in
            make.top.equalTo(topLayoutGuide.snp.bottom)
            make.leading.trailing.equalToSuperview()
        }
        
        explainView.snp.makeConstraints { make in
            make.top.equalTo(resultView.snp.bottom)
            make.leading.trailing.equalToSuperview()
        }
        
        sevenTimeChartView.snp.makeConstraints { make in
            make.top.equalTo(explainView.snp.bottom).offset(20)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20)
        }
        

    }
    
    override func bindViewModel() {
        super.bindViewModel()
        
        if let hrvModel = provider?.hrvModel {
            resultView.dateLabel.text = hrvModel.date.toString(.custom("home_workout_records_item_date_format".language))
            resultView.hrvLabel.text = "\(hrvModel.hrv)"
        }
        
        // 7次
        if let models = provider?.trend7TimesHRVModels {
            
            let barHightLightColor = UIColor.HeartRate.theme
            let barNormalColor = UIColor.HeartRate.theme
            
            let barValues: [SevenTimeBarChartView.BarValue] = models.map {
                
                SevenTimeBarChartView.BarValue(time: $0.end, data: Double($0.hrv))
            }
            
            let config = self.setupConfig(isEmpty: false)
            let datas = barValues.map { $0.data }
            
            let xValueFormatter = DayAxisValueDateFormatter()
            xValueFormatter.axisValueArray = datas.map {
                
                if $0 == 0 {
                    return ""
                }
                
                return "\(Int($0))"
                
            }

            let barModel = SevenTimeBarChartView.BarsModel(color: barNormalColor, hightLightColor: barHightLightColor, bars: barValues, xAxisValueFormatter: xValueFormatter, xConfig: config)
            
            sevenTimeChartView.barModel = barModel
            
        }
    }
    
    func setupConfig(isEmpty: Bool) -> DFBarChartViewConfig {
        let config = DFBarChartViewConfig.init(
            xAxisLabelFont: Fonts.systemFont(ofSize: 10),
            xAxisLabelTextColor: isEmpty == false ? UIColor.assist_222222 : UIColor.assist_FFFFFF,
            xAxisLineColor: UIColor.HRV.theme,
            xAxisLineHeight: 1,
            highlightPerTapEnabled: true)
        return config
    }
    

    private lazy var scrollView: ScrollView = {
        let view = ScrollView()
        view.showsVerticalScrollIndicator = false
        view.backgroundColor =  UIColor.darkBackground
        view.bounces = false
        return view
    }()

    private lazy var contentView: View = {
        let view = View()
        view.backgroundColor =  UIColor.darkBackground
        return view
    }()
    
    private lazy var resultView: SingleHRVRResultView = {
        let view = SingleHRVRResultView()
        view.backgroundColor = UIColor.background
        view.titleLabel.text = "hrv_title".language
        view.unitLabel.text = "unit_hrv".language
        view.dateLabel.text = "--"
        view.hrvLabel.text = "--"
        
        return view
    }()
    
    private lazy var explainTitleView: BloodOxygenDetailTitleView = {
        let explainTitleView = BloodOxygenDetailTitleView()
        explainTitleView.bind(to: "hrv_about_title".language, bgColor: UIColor.assist_FFFFFF)
        return explainTitleView
    }()
    
    private lazy var explainView: ExPlainView = {
        let explainView = ExPlainView()
        explainView.backgroundColor = UIColor.background
        explainView.updateData(
            textColor: UIColor.HRV.theme,
            title: "hrv_understand_data_tittle".language,
            explain: "hrv_understand_data_description_one".language + "\n\n" + "hrv_understand_data_description_two".language,
            explainTextColor: UIColor.HRV.theme.withAlphaComponent(0.8),
            bgColor: UIColor.HRV.theme)
        explainView.cornerRadius = 18.0
        return explainView
    }()
    
    private lazy var sevenTimeChartView: SevenTimeBarChartView = {
        let chartView = SevenTimeBarChartView()
        chartView.backgroundColor = UIColor.background
        chartView.cornerRadius = 18.0
        return chartView
    }()

}
